package com.sqfw.project.wpReport.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.sqfw.common.constant.HttpStatus;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.knowledge.domain.KlContentInfo;
import com.sqfw.project.knowledge.domain.KlHasNotwrong;
import com.sqfw.project.knowledge.mapper.KlContentInfoMapper;
import com.sqfw.project.knowledge.mapper.KlHasNotwrongMapper;
import com.sqfw.project.system.domain.SysRole;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysRoleMapper;
import com.sqfw.project.system.mapper.SysUserMapper;
import com.sqfw.project.wp.domain.WpHandleLog;
import com.sqfw.project.wp.mapper.WpHandleLogMapper;
import com.sqfw.project.wpReport.domain.WpInfoDetail;
import com.sqfw.project.zj.domain.ZjSoundCase;
import com.sqfw.project.zj.mapper.ZjSoundCaseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.sqfw.project.wpReport.domain.SupervisionWorkReport;
import com.sqfw.project.wpReport.service.ISupervisionWorkReportService;


/**
 * 督导工作情况汇总表Service业务层处理
 * 
 * @author lx
 * @date 2023-07-28
 */
@Slf4j
@Service
public class SupervisionWorkReportServiceImpl implements ISupervisionWorkReportService 
{

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private WpHandleLogMapper wpHandleLogMapper;

    @Autowired
    private KlContentInfoMapper klContentInfoMapper;

    @Autowired
    private ZjSoundCaseMapper zjSoundCaseMapper;

    @Autowired
    private KlHasNotwrongMapper klHasNotwrongMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    @Qualifier(value = "asyncServiceExecutor")
    private Executor executor;

    @Override
    public TableDataInfo getSupervisionWorkReportList(SupervisionWorkReport supervisionWorkReport) {

        List<Long> depts = new ArrayList<>();
        //质检组
        depts.add(105L);
        //知识库组
        depts.add(281L);
        //回访组
        depts.add(107L);
        //承接组
        depts.add(338L);

        SysUser sysUser = new SysUser();

        //sysUser.setRoleKeys(roleKeys);
        sysUser.setDeptIds(depts);
        sysUser.setCode(supervisionWorkReport.getCode());
        sysUser.setNickName(supervisionWorkReport.getNickName());

        List<SysUser> sysUsers = sysUserMapper.selectUserByDeptsAndCode(sysUser);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(new PageInfo(sysUsers).getTotal());
        //未查询到对应坐席人员,直接返回
        if (sysUsers.size() == 0) {
            tableDataInfo.setRows(new ArrayList<SupervisionWorkReport>());
            tableDataInfo.setMsg("查询成功");
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            return tableDataInfo;
        }

        List<String> nextStates = new ArrayList<>();
        nextStates.add("审核人处理");
        nextStates.add("承办单位处理");
        //创建异步阻塞任务集合
        List<CompletableFuture<SupervisionWorkReport>> futureList = new ArrayList<>();
        //初始化线程数量
       // CountDownLatch countDownLatch= ThreadUtil.newCountDownLatch(sysUsers.size());
        for (SysUser user : sysUsers) {
            log.info(user.getNickName());
            futureList.add(CompletableFuture.supplyAsync(() -> getSupervisionWorkListFutule(user, supervisionWorkReport, nextStates), executor));

        }

        //等待所有任务执行完成
        CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]));
        List<SupervisionWorkReport> collect = futureList.stream().map(CompletableFuture::join).collect(Collectors.toList());


        tableDataInfo.setRows(collect);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(HttpStatus.SUCCESS);

        return tableDataInfo;
    }

    private SupervisionWorkReport getSupervisionWorkListFutule(SysUser user ,SupervisionWorkReport supervisionWorkReport,List<String> nextStates) {

        log.info("用户名称：{}",user.getNickName());
        WpHandleLog handleLog = new WpHandleLog();

        handleLog.setBegainTime(supervisionWorkReport.getBegainTime());
        handleLog.setEndTime(supervisionWorkReport.getEndTime());

        KlHasNotwrong klHasNotwrong = new KlHasNotwrong();

        klHasNotwrong.setBegainTime(supervisionWorkReport.getBegainTime());
        klHasNotwrong.setEndTime(supervisionWorkReport.getEndTime());

        //查询文字审核数
        KlContentInfo klContentInfo = new KlContentInfo();
        klContentInfo.setBegainTime(supervisionWorkReport.getBegainTime());
        klContentInfo.setEndTime(supervisionWorkReport.getEndTime());

        ZjSoundCase zjSoundCase = new ZjSoundCase();
        zjSoundCase.setBegainTime(supervisionWorkReport.getBegainTime());
        zjSoundCase.setEndTime(supervisionWorkReport.getEndTime());

        List<SysRole> sysRoles = sysRoleMapper.selectRolePermissionByUserId(user.getUserId());

        SupervisionWorkReport report = new SupervisionWorkReport();
        report.setCode(user.getCode());
        report.setNickName(user.getNickName());

        String roleNames = sysRoles.stream().map(SysRole::getRoleName).collect(Collectors.joining("/"));

        report.setRoleName(roleNames);
        handleLog.setOperator(user.getCode());
        //督导审核通过
        handleLog.setLastState("督导处理");
        // handleLog.setNextState("审核人处理");
        handleLog.setNextStates(nextStates);
        List<String> ddExamine =  wpHandleLogMapper.countWpHandleLog(handleLog);

        handleLog.setNextStates(null);
        report.setDdExamine(ddExamine.size());
        //督导退回坐席
        handleLog.setLastState("督导处理");
        handleLog.setNextState("坐席处理");

        List<String> ddToZx =  wpHandleLogMapper.countWpHandleLog(handleLog);
        report.setDdToZx(ddToZx.size());

        //审核人退回督导
        handleLog.setLastState("审核人处理");
        handleLog.setNextState("督导处理");

        List<String> shrToDd =  wpHandleLogMapper.countWpHandleLog(handleLog);
        report.setShrToDd(shrToDd.size());

        //质检退回坐席
        handleLog.setLastState("质检审核处理");
        handleLog.setNextState("坐席处理");

        List<String> zjToZx =  wpHandleLogMapper.countWpHandleLog(handleLog);
        report.setZjToZx(zjToZx.size());

        //质检审核通过
        handleLog.setLastState("质检审核处理");
        handleLog.setNextState(null);
        List<String> selfCount =  wpHandleLogMapper.countWpHandleLogToZJ(handleLog);
        report.setSelfCount(selfCount.size());

        //培训助理已处理
        handleLog.setLastState("培训助理处理");

        List<String> trainCount =  wpHandleLogMapper.countWpHandleLog(handleLog);
        report.setTrainCount(trainCount.size());

        //文字案例审核数
        klContentInfo.setKlContentReviewer(user.getUserId().toString());
        int textCaseCount =  klContentInfoMapper.countKlContentInfoByReviewer(klContentInfo);
        report.setTextCaseCount(textCaseCount);

        //声音案例审核数
        zjSoundCase.setReviewedBy(user.getCode());
        int soundCaseCount =  zjSoundCaseMapper.countZjSoundCaseByReviewer(zjSoundCase);
        report.setSoundCaseCount(soundCaseCount);

        //查无纠错发起数
        klHasNotwrong.setKlHasCreateperson(user.getUserId().toString());
        int cwSendCount = klHasNotwrongMapper.selectCwSendCount(klHasNotwrong);

        report.setCwSendCount(cwSendCount);

        int cwProcessCount = klHasNotwrongMapper.selectCwProcessCount(klHasNotwrong);
        report.setCwProcessCount(cwProcessCount);

        return report;

    }
}
